/*******************************************************************************
	   Authors: Dipika Gawande
	   Project: ETS
	   Purpose: Make balance tables

	   Date created: 25 November 2020
	   Version:      STATA 15 MP

	   Last edited: 30 June 2022
	   Edited by:   Jeanne, Vineet, Kaixin
   ****************************************************************************/
   
set more off
clear matrix
clear all
pause on

use "$BASELINE_DATA_OUT/BaselineCovariates.dta", clear
do "$CODE_DIR/diff_test.do"

********************************************************************************
************************ [1] BALANCE CHECK (FULL SAMPLE) ***********************
********************************************************************************

gen D_small_scale = (size == 1)
replace D_small_scale = . if size == .
gen D_large_scale = (size == 3)
replace D_large_scale = . if size == .
replace boi_year = . if D_boi_year_missing == 1
replace bh_total_workers = . if D_bh_total_workers_missing == 1
replace bh_ope_cost_USD = . if D_bh_ope_cost_missing == 1
replace bh_total_capex_USD = . if D_bh_total_capex_missing == 1
replace grossrev_17_18_clean_USD = grossrev_17_18_clean_USD / 1000
replace bh_total_capex_USD = bh_total_capex_USD / 1000
replace bh_ope_cost_USD = bh_ope_cost_USD / 1000
replace elecbill_2017_clean_USD = elecbill_2017_clean_USD / 1000
gen above_standard = pm_conc_etsbl > 150
replace above_standard = . if pm_conc_etsbl == .


local cov_interest_A = "elecbill_2017_clean_USD ln_plant_total_heatoutput size D_small_scale D_large_scale pm_cnsnt_stcks D_textiles grossrev_17_18_clean_USD"
local cov_interest_B = "bh_total_workers bh_total_capex_USD bh_ope_cost_USD D_cyc D_bf D_scr D_esp"
local cov_interest_C = "pm_mass_etsbl pm_conc_etsbl mean_ring_premock above_standard"

label var grossrev_17_18_clean_USD "Gross Sales Revenue in 2017 (1,000 USD)"
label var elecbill_2017_clean_USD "Total electricity cost (1,000 USD)"
label var ln_plant_total_heatoutput "Log(plant total heat output)"
label var size "Size as recorded on environment consent (1 to 3)"
label var D_small_scale "Small-scale (size=1)"
label var D_large_scale "Large-scale (size=3)"
label var pm_cnsnt_stcks "Number of stacks"
label var D_textiles "Textiles sector (=1)"
label var bh_total_workers "Boiler house employment"
label var bh_total_capex_USD "Boiler house capital expenditure (1,000 USD)"
label var bh_ope_cost_USD "Boiler house operating cost (1,000 USD)"
label var D_cyc "APCD: Cyclone present"
label var D_bf "APCD: Bag ﬁlter present"
label var D_scr "APCD: Scrubber present"
label var D_esp "APCD: ESP present"
label var pm_mass_etsbl "Plant total PM mass rate (kg/hr)"
label var pm_conc_etsbl "Plant mean PM concentration (mg/Nm\textsuperscript{3})"
label var mean_ring_premock "Plant mean Ringelmann score (1 to 5)"
label var above_standard "Above regulatory standard at ETS baseline (=1)"

diffTest3 `cov_interest_A', by(D_treatment)
esttab using "$EMISSIONS_TABS/Table_A4.tex", replace fragment ///
	noobs nomtitles nonumbers collabels("Treatment" "Control" "Difference") ///
	varwidth(32) label nogap compress booktabs width(0.75\hsize) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead( ///
		"\begin{longtable}{lccc}" ///
		"\caption{Balance of plant characteristics by treatment status, full sample}" ///
		"\label{tab:balanceFull} \\" ///
		"\toprule") ///
	posthead( ///
	  "&(1)&(2)&(3)\\" ///
		"\midrule" ///
		"\addlinespace" ///
		"\multicolumn{4}{c}{\textit{Panel A: Plant Measures}} \\" ) /// 
	postfoot("\addlinespace\addlinespace")
		
diffTest3 `cov_interest_B', by(D_treatment)
esttab using "$EMISSIONS_TABS/Table_A4.tex", append fragment ///
	noobs nomtitles nonumbers collabels(none) ///
	varwidth(32) label nogap booktabs width(0.75\hsize) eqlabel(none) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead("\multicolumn{4}{c}{\textit{Panel B: Plant Abatement and Investment Cost}} \\") ///
	posthead("") /// 
	prefoot("") ///
	postfoot("\addlinespace\addlinespace")
	
diffTest3 `cov_interest_C', by(D_treatment)
esttab using "$EMISSIONS_TABS/Table_A4.tex", append fragment ///
	noobs nomtitles nonumbers collabels(none) ///
	varwidth(32) label  nogap booktabs width(0.75\hsize) eqlabel(none) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead("\multicolumn{4}{c}{\textit{Panel C: Plant Pollution Measures}} \\") ///
	posthead("") /// 
	prefoot("\addlinespace\addlinespace") ///
	postfoot( ///
		"Number of plants & 162 & 156 & \\" ///
		"\bottomrule\\[-5.2ex]" ///
		"\insertTableNotes" ///
		"\end{longtable}" )
		

di "Data for Table A2: Distribution of number of stacks by plant"
tab pm_cnsnt_stcks
tab pm_cnsnt_stcks if D_treatment == 1
tab pm_cnsnt_stcks if D_treatment == 0

********************************************************************************
********************** [2] BALANCE CHECK (ANALYSIS SAMPLE) *********************
********************************************************************************

merge 1:m gpcb_id using "$IMPUTATION_DATA_OUT/PlantMonthPMMassRule0.dta", 
keep if _merge == 3
bysort industry_id: gen dump=_n
bysort industry_id: keep if _n==_N
 	
diffTest3 `cov_interest_A', by(D_treatment)
esttab using "$EMISSIONS_TABS/Table_1.tex", replace fragment ///
	noobs nomtitles nonumbers collabels("Treatment" "Control" "Difference") ///
	varwidth(32) label nogap compress booktabs width(0.75\hsize) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead( ///
		"\begin{longtable}{lccc}" ///
		"\caption{Balance of plant characteristics by treatment status}" ///
		"\label{tab:balance} \\" ///
		"\toprule") ///
	posthead( ///
	  "&(1)&(2)&(3)\\" ///
		"\midrule" ///
		"\addlinespace" ///
		"\multicolumn{4}{c}{\textit{Panel A: Plant Measures}} \\" ) /// 
	postfoot("\addlinespace\addlinespace")
		
diffTest3 `cov_interest_B', by(D_treatment)
esttab using "$EMISSIONS_TABS/Table_1.tex", append fragment ///
	noobs nomtitles nonumbers collabels(none) ///
	varwidth(32) label nogap booktabs width(0.75\hsize) eqlabel(none) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead("\multicolumn{4}{c}{\textit{Panel B: Plant Abatement and Investment Cost}} \\") ///
	posthead("") /// 
	prefoot("") ///
	postfoot("\addlinespace\addlinespace")
	
diffTest3 `cov_interest_C', by(D_treatment)
esttab using "$EMISSIONS_TABS/Table_1.tex", append fragment ///
	noobs nomtitles nonumbers collabels(none) ///
	varwidth(32) label  nogap booktabs width(0.75\hsize) eqlabel(none) ///
	cells("mu_1(fmt(a2))          mu_2           d(star pvalue(d_p))" ///
		  "sd_1(fmt(a2) par([ ])) sd_2(par([ ])) d_se(par)") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	prehead("\multicolumn{4}{c}{\textit{Panel C: Plant Pollution Measures}} \\") ///
	posthead("") /// 
	prefoot("\addlinespace\addlinespace") ///
	postfoot( ///
		"Number of plants & 156 & 136 & \\" ///
		"\bottomrule\\[-5.2ex]" ///
		"\insertTableNotes" ///
		"\end{longtable}" )
